<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Newsletter extends CI_Controller
{
    protected $sesja= null;
    protected $log  = null;
    protected $elems= null;
    
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('Newsletter_model','nl');
        $this->sesja= $this->session->userdata('sesja');
        $this->log  = new Logevent();
        $this->elems= new Elements();
    }
    
    public function write_newsletter()
    {
        if(isset($this->sesja['id']) && $this->nl->can_mail($this->sesja['id']))
        {
            $head['h1'] = 'Napisz nowy newsletter';
            $lista      = $this->nl->list_users(); 
            $foot['js'] = array(
                'ckeditor/ckeditor.js',
                'ckeditor/adapters/jquery.js',
                'js/default/ckeditor.config.js'
            );
            if($lista['stan'])
            {
                $cont['lista'] = $lista['wynik'];
                $this->elems->load_view($this->sesja,'newsletter/create_newsletter',$cont,$head,$foot);
            }
            else
                $this->elems->load_alert($this->sesja,'error','Brak użytkowników w systemie. Dlaczego więc jesteś w systemie? Hakujesz?');
        }
        else
            $this->elems->load_alert($this->sesja,'error','Nie masz uprawnień do korzystania z tej strony');
    }
    
    public function send_newsletter()
    {
        if(isset($this->sesja['id']) && $this->nl->can_mail($this->sesja['id']))
        {
            $this->load->library(array('form_validation','email'));
            //mpr($_POST,1);
            $this->form_validation->set_error_delimiters('<p class="error">','</p>');
            $this->form_validation->set_rules('title','Tytuł wiadomości','required|xss_clean');
            $this->form_validation->set_rules('content','Treść wiadomości','required|xss_clean');
            
            if($this->form_validation->run() == FALSE && isset($_POST['send']))
                $this->write_newsletter();
            else
            {
                //pretty_print($_POST);
                $isall = $this->input->post('sall');
                if(isset($isall) && !empty($isall))
                {
                    foreach($isall as $i=>$v)
                    {
                        $email = $this->nl->get_mail($i);
                        /*$this->email->clear();
                        
                        $this->email->to($email);
                        $this->email->from('info@unitedteam.pl', $this->sesja['name']);
                        $this->email->subject($this->input->post('title'));
                        $this->email->message($this->input->post('content'));
                        if(!$this->email->send())
                            $this->elems->load_alert($this->sesja,'error',$this->email->print_debugger());*/
                    }
                    $this->elems->load_alert($this->sesja,'success','Wysyłka wiadomości zakończona sukcesem!');
                }
                else
                {
                    $emails = $this->nl->get_mails();
                    foreach($emails as $e)
                    {
                        $this->email->clear();
                        $this->email->to($e);
                        $this->email->from('info@unitedteam.pl', $this->sesja['name']);
                        $this->email->subject($this->input->post('title'));
                        $this->email->message($this->input->post('content'));
                        if(!$this->email->send())
                            $this->elems->load_alert($this->sesja,'error',$this->email->print_debugger());
                    }
                    $this->elems->load_alert($this->sesja,'success','Wysyłka wiadomości zakończona sukcesem!');
                }
            }
        }
        else
            $this->elems->load_alert($this->sesja,'error','Nie masz uprawnień do korzystania z tej strony');
    }
    
    public function dashboard()
    {
        if(isset($this->sesja['id']) && $this->nl->can_mail($this->sesja['id']))
        {
            /*
             * ładowanie biblioteki do obsługi poczty email
             */
            $this->load->library('peeker/peeker','mail');
            $mail = new peeker();
            $config['login']= $this->config->item('imap_login');
            $config['pass'] = $this->config->item('imap_pass');
            $config['host'] = $this->config->item('imap_host');
            $config['port'] = $this->config->item('imap_port');
            $config['service_flags'] = $this->config->item('imap_flags');
            $mail->initialize($config);
            
            if($mail->message_waiting())
            {
                $head['h1']         = 'Skrzynka odbiorcza';
                $num_emails         = $mail->get_message_count();
                $cont['num_mails']  = 'Masz '.$num_emails.(($num_emails>1) ? ' wiadomości' : ' wiadomość').' w skrzynce';
                $foot['js']     = array(
                    'js/newsletter/newsletter_dashboard.js'
                );
                //$cont['emails']     = $mail->get_message(1,$num_emails);
                for($i=1;$i<=$num_emails;$i++)
                {
                    $email = $mail->get_message($i);
                    $cont['emails'][$email->get_msgno()]['date'] = $email->get_date();
                    $cont['emails'][$email->get_msgno()]['subj'] = $email->get_subject();
                    $cont['emails'][$email->get_msgno()]['from'] = $email->get_from();
                    $cont['emails'][$email->get_msgno()]['autor']= $email->get_from_array('mailbox@host');
                    $cont['emails'][$email->get_msgno()]['body'] = ($email->has_PLAIN_not_HTML() ? iconv('ISO-8859-2','UTF-8',$email->get_plain()) : iconv('ISO-8859-2','UTF-8',$email->get_html()));
                    $cont['emails'][$email->get_msgno()]['repl'] = $email->get_reply_to();
                    $cont['emails'][$email->get_msgno()]['arra'] = $email->get_from_array('mailbox@host');
                }
                $this->elems->load_view($this->sesja,'newsletter/dashboard',$cont,$head,$foot);
            }
            else
            {
                $alert = pretty_print(imap_errors());
                $this->elems->load_alert($this->sesja,'error',$alert);
            }
            $this->peeker->close();
        }
        else
            $this->elems->load_alert($this->sesja,'error','Nie masz uprawnień do korzystania z tej strony');
    }
}
